package com.watson.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.watson.R;

import java.util.List;
import java.util.Map;

public class SetAdapter extends RecyclerView.Adapter<SetAdapter.ItemViewHolder> {
    private ItemClickListener mItemClick;
    private Context mContext;
    private List<Map> mList;

    public SetAdapter(Context mContext, List<Map> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    public void setData(List<Map> vList) {
        this.mList = vList;
        notifyDataSetChanged();
    }

    public void setItemClickListener(ItemClickListener listener) {
        this.mItemClick = listener;
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder itemViewHolder = new ItemViewHolder(LayoutInflater
                .from(mContext).inflate(R.layout.itme_set, parent, false));
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, final int position) {
        if (mList.size() > 0) {
            holder.tvName.setText(mList.get(position).get("name") + "");
            holder.tvContext.setText("巡查内容：" + mList.get(position).get("context"));
            holder.tvTime.setText("开始：" + mList.get(position).get("startTime") + "\n截止：" + mList.get(position).get("endTime"));
            //1：未完成，2：进行中，3：已完成
            int type = Integer.valueOf(mList.get(position).get("type") + "");
            switch (type) {
                case 1:
                    holder.tvType.setText("未完成");
                    holder.tvType.setTextColor(mContext.getResources().getColor(R.color.red_deep));
                    break;
                case 2:
                    holder.tvType.setText("进行中");
                    holder.tvType.setTextColor(mContext.getResources().getColor(R.color.bg_main));
                    break;
                case 3:
                    holder.tvType.setText("已完成");
                    holder.tvType.setTextColor(mContext.getResources().getColor(R.color.tv333));
                    break;
            }
            holder.itemView.setOnClickListener(v -> {
                if (mItemClick != null) {
                    mItemClick.onClick(position, mList);
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName, tvTime, tvType, tvContext;

        public ItemViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvTime = itemView.findViewById(R.id.tvTime);
            tvType = itemView.findViewById(R.id.tvType);
            tvContext = itemView.findViewById(R.id.tvContext);
        }
    }

    public interface ItemClickListener {
        void onClick(int position, List<Map> mList);
    }
}
